import http, {ApiResponse, ApiResponsePage} from "../../../../lang/http";

export interface Role {
    // ID
    id: string;
    // 机构ID
    orgId: string;
    // 授权码
    authorityCode: string;
    // 角色名称
    roleName: string;
    // 角色图标
    roleIcon: string;
    // 是否可用
    disabled: boolean;
    // 是否可编辑
    editable: boolean;
    // 描述
    description: string;
}

const formModel = (): Role => {
    return {
        id: '',
        orgId: '',
        authorityCode: '',
        roleName: '',
        icon: '',
        disabled: false,
        editable: false,
        description: '',
    }
}

const formRules = {
    id: [
        {required: true, message: '[ID]字段不允许为空', trigger: 'blur'},
        {min: 0, max: 32, message: '最多32字符', trigger: 'blur'}
    ],
    orgId: [
        {required: true, message: '[机构ID]字段不允许为空', trigger: 'blur'},
        {min: 0, max: 32, message: '最多32字符', trigger: 'blur'}
    ],
    authorityCode: [
        {required: true, message: '[授权码]字段不允许为空', trigger: 'blur'},
        {min: 0, max: 32, message: '最多32字符', trigger: 'blur'}
    ],
    roleName: [
        {required: true, message: '[角色名称]字段不允许为空', trigger: 'blur'},
        {min: 0, max: 64, message: '最多64字符', trigger: 'blur'}
    ],
    icon: [
        {required: true, message: '[]字段不允许为空', trigger: 'blur'},
        {min: 0, max: 255, message: '最多255字符', trigger: 'blur'}
    ],
    description: [
        {required: true, message: '[描述]字段不允许为空', trigger: 'blur'},
        {min: 0, max: 128, message: '最多128字符', trigger: 'blur'}
    ]
    // disabled, editable, gmtCreate, gmtModified 没有设置规则
}


/**
 * 数据新增
 *
 * @param params 对象实体
 * @returns 请求Promise
 */
const create = (params: Role): Promise<ApiResponse> => {
    return http.ajax({
        url: '/sys/role/info',
        method: 'POST',
        params
    });
}

/**
 * 根据 ID 删除
 *
 * @param id 数据 ID
 * @returns 请求Promise
 */
const remove = (id: string | number): Promise<ApiResponse> => {
    return http.ajax({
        method: "DELETE",
        url: 'sys/role/info',
        params: {id}
    });
}

/**
 * 数据修改
 *
 * @param params 对象实体
 * @returns 请求Promise
 */
const modify = (params: Role): Promise<ApiResponse> => {
    return http.ajax({
        method: "PUT",
        url: 'sys/role/info',
        params: params
    });
}

/**
 * 数据列表查询
 *
 * @param params 查询参数
 * @returns 分页数据Promise
 */
const list = (params: object): Promise<Role[]> => {
    return http.ajax({
        url: '/sys/role/list',
        method: 'GET',
        params
    });
}

export default {create, remove, modify, list, formModel, formRules}